package org.nerve.webmagic;

import com.machinepublishers.jbrowserdriver.JBrowserDriver;
import com.machinepublishers.jbrowserdriver.RequestHeaders;
import com.machinepublishers.jbrowserdriver.Settings;
import com.machinepublishers.jbrowserdriver.UserAgent;
import org.nerve.webmagic.bean.SpiderConfig;
import org.openqa.selenium.Dimension;

import java.io.IOException;

/**
 * org.nerve.webmagic
 * Created by zengxm on 2016/4/21 0021.
 */
public class JBrowserDriverBuilder implements DriverBuilder<JBrowserDriver> {
	@Override
	public JBrowserDriver build(SpiderConfig config) throws IOException {
		Settings.Builder builder=Settings.builder()
				.javascript(true)
				.userAgent(UserAgent.CHROME)
				.screen(new Dimension(1400,720))    //经过测试，height如果超过750，使用ashot将无法截图。。
				.connectTimeout(3*60*1000)
				.requestHeaders(RequestHeaders.CHROME)
				;
		//如果拍照的话，就加载图片
		if(config.isTakeScreenshot())
			builder.quickRender(false);

		JBrowserDriver driver=new JBrowserDriver(builder.build());
		return driver;
	}
}
